package com.project.nimsen.web.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.jdbc.utils.DateUtils;
import com.project.jdbc.utils.Page;
import com.project.jdbc.web.BaseMVCAction;
import com.project.nimsen.dto.RoleDto;
import com.project.nimsen.entity.Role;
import com.project.nimsen.entity.User;
import com.project.nimsen.service.IRoleService;
import com.project.nimsen.util.WebUtil;
/**
 * 角色Control
 * */
@Controller
@RequestMapping(value ="/sys/role")
public class RoleControl extends BaseMVCAction {
	@Resource
	IRoleService roleService;
	@Resource
	WebUtil webUtil;
	@RequestMapping(value ="/list")
	public String list(){
		return "/WEB-INF/sys/role/list";
	}
	/**
	 * 验证角色是否存在
	 * */
	@RequestMapping(value ="/validate")
	@ResponseBody
	public Map<String, Object> validate(){
		Map<String,Object> map = new HashMap<String,Object>();
		RoleDto dto = new RoleDto();
		dto.setValifield("rolename");
		dto.setValivalue(getParameter("rolename"));
		dto.setId(getParameter("id"));
		try {
			StringBuffer sbfMessage = new StringBuffer();
			boolean validate = true;
			Map<String, Object> authnameMap = roleService.findVilidate(dto);
			if(authnameMap!=null){
				validate = false;
				sbfMessage.append(" 角色名称");
			}
			if(!validate)
				map.put("message", "已经丰在角色名为"+dto.getValivalue()+"的角色！");
			else
				map.put("message", "");
			map.put("success", validate);
		} catch (Exception e) {
			map.put("success", false);
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 添加方法
	 * */
	@RequestMapping(value="add")
	public void add(HttpServletResponse res){
		User use = webUtil.getCurrUser();
		String id = getParameter("id");
		String rolename = getParameter("rolename");
		Role role = null;
		try {
			if(StringUtils.isBlank(id)){
				role = new Role();
				role.setCreateUser(use.getId());
				role.setCreateTime(new Date());
				role.setActive(0);
				role.setRolename(rolename);
				roleService.save(role);
			}else{
				role = roleService.findById(id);
				role.setRolename(rolename);
				roleService.update(role);
			}
			res.getWriter().println(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除方法
	 * */
	@RequestMapping(value="delete")
	@ResponseBody
	public Map<String, Object> delete(){
		Map<String, Object> map = new HashMap<String, Object>();
		String id = getParameter("id");
		if(StringUtils.isBlank(id)){
			map.put("success", false);
			return map;
		}
		try {
			map.put("success", roleService.deleteById(id));
		} catch (Exception e) {
			map.put("success", false);
			e.printStackTrace();
		}
		//success
		return map;
	}
	/**
	 * 查询方法
	 * */
	@RequestMapping(value="search")
	@ResponseBody
	public Map<String, Object> search(){
		//分页信息
		Page page = new Page(getIntegerParameter("page"));
		page.setPageSize(getIntegerParameter("rows"));
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", 0);
		RoleDto dto = new RoleDto();
		dto.setPage(page);
		try {
			List<Map<String, Object>> rows = roleService.findByDto(dto);
			if(CollectionUtils.isEmpty(rows)){
				map.put("rows", "");
				return map;
			}
			//转换时间
			for(Map<String, Object> row : rows){
				Date createTime = (Date)row.get("createTime");
				row.put("createTime", DateUtils.convertDate2String(createTime));
			}
			map.put("rows", rows);
			map.put("total", page.getTotalCount());
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
}
